package com.wing.cabinet.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.Cabinet;
import com.wing.cabinet.model.response.CellSingleInventoryProductVO;
import com.wing.cabinet.service.CellSingleInventoryProductService;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.cabinet.model.entity.CabinetCargoLane;
import com.wing.cabinet.model.response.CabinetCargoLaneVO;
import com.wing.cabinet.service.CabinetCargoLaneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.cabinet.model.request.CabinetCargoLaneForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-刀柜货道管理", tags = "刀柜货道管理")
@RestController("appCabinetCargoLaneController")
@RequestMapping("cabinet/app/cabinetCargoLane")
public class CabinetCargoLaneController {

    @Autowired
    private CabinetCargoLaneService cabinetCargoLaneService;
    @Autowired
    private LocaleMessage localeMessage;

    @Autowired
    private CellSingleInventoryProductService cellSingleInventoryProductService;

    @ApiOperation(value = "查询所有刀柜货道", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabinetId", value = "柜子id", defaultValue = "", example = "", dataType = "Long", paramType = "query")

    })
    @GetMapping("/list")
    public PageResult<CabinetCargoLaneVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "cast(t.code as unsigned)") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String code
            ,String cabCode
            ,Long cabinetId
            ,String queryFlag
            ,Long receiveId
            ,String skuCode
            ,String cabinetCode
            ,String wsFlag
    ) {
        Page<CabinetCargoLaneVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if (StringUtils.isNotBlank(code)) {
            params.put("code", code);
        }
//        if(StringUtils.isNotBlank(cabCode)){
//            params.put("cabinetCode",cabCode);
//        }
        params.put("cabinetCode",cabinetCode);
        params.put("cabCode",cabCode);
        params.put("cabinetId",cabinetId);
        params.put("queryFlag",queryFlag);
        params.put("receiveId",receiveId);
        params.put("skuCode",skuCode);
        if (StringUtils.isBlank(wsFlag)){
            wsFlag= Cabinet.CabinetType.spring.name();
        }
        params.put("wsFlag",wsFlag);
        Page<CabinetCargoLaneVO> list = cabinetCargoLaneService.pageList4app(pg, params);

        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询所有刀柜货道", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "cabinetId", value = "柜子id", defaultValue = "", example = "", dataType = "Long", paramType = "query")

    })
    @GetMapping("/list4Load")
    public PageResult<CabinetCargoLaneVO> list4Load(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "cast(t.code as unsigned)") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String code
            ,String cabCode
            ,Long cabinetId
            ,String queryFlag
            ,Long receiveId
            ,String skuCode
            ,String cabinetCode
            ,String wsFlag
            ,String name
            ,Long productTypeId
            ,Long productCategoryId
    ) {
        Page<CabinetCargoLaneVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if (StringUtils.isNotBlank(code)) {
            params.put("code", code);
        }
//        if(StringUtils.isNotBlank(cabCode)){
//            params.put("cabinetCode",cabCode);
//        }
        params.put("cabinetCode",cabinetCode);
        params.put("cabCode",cabCode);
        params.put("cabinetId",cabinetId);
        params.put("queryFlag",queryFlag);
        params.put("receiveId",receiveId);
        params.put("skuCode",skuCode);
        if (StringUtils.isBlank(wsFlag)){
            wsFlag= Cabinet.CabinetType.spring.name();
        }
        params.put("wsFlag",wsFlag);
        params.put("productTypeId",productTypeId);
        params.put("productCategoryId",productCategoryId);
        params.put("name",name);
        Page<CabinetCargoLaneVO> list = cabinetCargoLaneService.pageList4app(pg, params);
        List<CellSingleInventoryProductVO> alarmList=cellSingleInventoryProductService.list4Alarm(new HashMap<>());
        Map alarmMap=new HashMap();
        alarmList.forEach(e->{
            alarmMap.put(e.getBelongCabinet()+"_"+e.getSkuCode(),e.getBelongCabinet()+e.getSkuCode());
        });
        list.getRecords().forEach(e->{
            if (alarmMap.containsKey(e.getMainCabCode()+"_"+e.getSkuCode())){
                e.setAlarm(true);
            }
        });
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/detail/{id}")
    public JsonResult detail(@PathVariable("id") Long cabinetCargoLaneId) {
        CabinetCargoLaneVO cabinetCargoLaneVO = cabinetCargoLaneService.get4appById(cabinetCargoLaneId);
        if (cabinetCargoLaneVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), cabinetCargoLaneVO);
    }

    @PostMapping()
    public JsonResult add(@Validated CabinetCargoLaneForm cabinetCargoLaneForm) {
        if (cabinetCargoLaneService.add(cabinetCargoLaneForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }
    @ApiOperation(value = "获取可用的货道", notes = "搜索")
    @GetMapping("/getUsableSpaces")
    public JsonResult<List<CabinetCargoLaneVO>> getUsableSpaces(String cabCode, String skuCode, Integer quantity){
        if (quantity==null){
            quantity=1;
        }
        List<CabinetCargoLaneVO> laneVOS=cabinetCargoLaneService.getUsableSpaces(cabCode,skuCode,quantity);
        if (laneVOS.size()==0){
            return JsonResult.fail("无可用存放此数量的的货道");
        }
        return JsonResult.success(laneVOS);
    }

    @ApiOperation(value = "获取补录(取货)货道", notes = "搜索")
    @GetMapping("/getSkuSpaces")
    public JsonResult<List<CabinetCargoLaneVO>> getSkuSpaces(String cabCode){
        List<CabinetCargoLaneVO> list=cabinetCargoLaneService.getSkuSpaces(cabCode);
        return JsonResult.success(list);
    }
}
